﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using Newtonsoft.Json.Linq;
using Pap.Services.WorkflowEngine;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ExcutionOldDateControl : BaseUserControl<ExcutionOldDateControlViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest && !Page.IsPostBack)
            {
                var actionList = from action in this.ViewModel.GetAvailableDecisions()
                                 select new
                                 {
                                     DecisionID = Enums.ToString(action),
                                     DecisionName = Enums.ToString(action, GetLocalResourceObject("LanguageIndex").ToInt32())
                                 };

                storeExecutionDecisions.DataSource = actionList;
                storeExecutionDecisions.DataBind();

                storeOldExecuteLoans.DataSource = this.ViewModel.GetOldDateExcutedLoans();
                storeOldExecuteLoans.DataBind();
            }
        }


        /// <summary>
        /// Handles the Clicked event of the BtnSubmit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnSubmit_Clicked(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["grvExecutionLoans"] != null)
            {
                string gridString = e.ExtraParams["grvExecutionLoans"].ToString();
                JArray jArr = JSON.Deserialize(gridString) as JArray;

                Dictionary<string, int> actionTypes = Enums.ToNameKeyPaires(typeof(ActionTypes));

                List<ActionTypes> loanDecisions = new List<ActionTypes>();
                List<Guid> loansIDs = new List<Guid>();
                List<string> Notes = new List<string>();

                for (int i = 0; i < jArr.Count; i++)
                {
                    if (!string.IsNullOrEmpty(jArr[i]["DecisionID"].ToString().Trim('"')))
                    {
                        loanDecisions.Add((ActionTypes)actionTypes[jArr[i]["DecisionID"].ToString().Trim('"')]);
                        loansIDs.Add(jArr[i]["LoanID"].ToString().Trim('"').ToGuid());
                        if (jArr[i]["Notes"] != null)
                            Notes.Add(jArr[i]["Notes"].ToString().Trim('"'));
                        else
                            Notes.Add(string.Empty);
                    }
                }

                if (loanDecisions.Count > 0)
                {
                    this.ViewModel.TakeLoanDecisions(loansIDs, loanDecisions, Notes);
                    storeOldExecuteLoans.DataSource = this.ViewModel.GetOldDateExcutedLoans();
                    storeOldExecuteLoans.DataBind();
                }

            }
        }

    }
}